import axios from 'axios';
// import QS from 'qs'
import { getKey } from '@/utils/tools.js'

axios.defaults.baseURL = ' '
export function request({ method, params, url, type }) {

    if (method == "get") {
        return get(params, url, type)
    } else if (method == "post") {
        return post(params, url, type)
    }
}

var get = function(params, url, type) {
    return new Promise((resolve, reject) => {
        // let data = {};
        let data = params || "";
        let header = ''
        if (type) {
            let a = getKey('token')
            header = { 'Authorization': a }
        }

        axios({
            method: 'get',
            headers: header,
            url: '/api' + url,
            params: data
        }).then(res => {
            console.log(res)
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
var post = function(params, url, type) {
    console.log(params, url)
    return new Promise((resolve, reject) => {
        let header = ''
        if (type) {
            let a = getKey('token')
            header = { 'Content-Type': 'application/json', 'Authorization': a }
        }
        axios({
            method: 'post',
            headers: header,
            url: '/api' + url,
            data: params
        }).then(res => {
            console.log(res)
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
